// pages/shop/orderDetail/orderDetail.js
const call = require('../../utils/request.js')
const app = getApp();
let hasVip = false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    countDown:{
      min:0,
      sec:0
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orderType: options.type
    })
    this.getDetail(options.id,options.type);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取订单详情
   */
  getDetail(id,type){
    wx.showLoading({
      title: '正在获取数据',
      mask:true
    })
    call.post('/api/shop/order_detail',{
      order_product_id:id,
      type:type
    }).then(res=>{
      console.log("获取订单详情",res.data)
      if(type==1){
        this.countDown(res.data.data.pay_time);
        let arr = res.data.data.order_products;
        for(let i of arr){
          if(i.is_vip_product == 1){
            hasVip = true;
            break;
          }
        }
      }
      this.setData({
        data: res.data.data
      })
      wx.hideLoading();
    })
  },

    /**
   * 客服咨询
   */
  callPhone(){
    wx.makePhoneCall({
      phoneNumber: '18073907266',
      success(){console.log(1)},
      fail(){console.log(2)},
      complete(){console.log(3)}
    })
  },

  /**
   * 一键复制
   */
  copy(){
    const data = this.data.data;
    let str = '';
    if(this.data.orderType == 1){
      str = `${data.trade_no}`;
    }else{
      str = `${data.order.trade_no}`
    }
    wx.setClipboardData({
      data: str
    })
  },

  /**
   * 页面跳转
   */
  toUrl(e){
    const url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },

  /**
   * 未支付30分钟倒计时 
   */
  countDown(time){
    time = time.replace(/-/g,"/");
    let date = new Date(new Date(time).getTime() + (30*60*1000));
    let nowDate = new Date();
    let count = Math.floor(date-nowDate)/1000;
    console.log(count)
    let timer = setInterval(()=>{
      count-=1;
      this.setData({
        countDown:{
          min: Math.floor((count/60)%60),
          sec: Math.floor(count%60)
        }
      })
      if(Math.floor(count)==0){
        clearInterval(timer);
        let pages = getCurrentPages();
        let prePage = pages[pages.length - 2];
        prePage.getOrderList(prePage.data.orderName);
        wx.navigateBack({
          delta: 1,
        })
      }
    },1000);
  },

  /**
   * 确认收货
   */
  qrsh(e){
    const id = e.currentTarget.dataset.idx;
    call.post('/api/shop/confirm_obtain_goods',{
      order_product_id: id
    }).then(res=>{
      console.log("确认收货",res)
      //只刷新当前第几页的数据，不必将页数改为1
      this.getOrderList(this.data.orderName);
    })
  },

  /**
   *  查看物流 
   */
  watchWl(){
    wx.showToast({
      title: '功能暂未开放',
      icon:'none'
    })
  },

  /**
   *  取消订单 
   */
  cancelOrder(){
    wx.showToast({
      title: '功能暂未开放',
      icon:'none'
    })
  },

  /**
   *  支付 
   */
  toPay(){
    wx.showLoading({
      title: '正在提交订单',
      mask:true
    })
    call.post('/api/shop/pay',{
      order_id: this.data.data.id
    }).then(res=>{
      wx.hideLoading();
      console.log("发起支付",res)
      const res1 = res.data.data;
      if(res.statusCode == 200 && res.data.code == 0){
        wx.requestPayment({
          timeStamp: res1.timeStamp,
          nonceStr: res1.nonceStr,
          package: res1.package,
          signType: res1.signType,
          paySign: res1.paySign,
          success :(res) =>{ 
            console.log("调用成功")
            if(hasVip && app.globalData.vip == 0){
              app.globalData.vip = 1;
              let user = wx.getStorageSync('user');
              user.vip = 1;
              wx.setStorageSync('user', user)
            }
            wx.showLoading({
              title: '正在跳转',
              mask: true
            })
            setTimeout(function(){
              let pages = getCurrentPages();
              let prePage = pages[pages.length - 2];
              prePage.getOrderList(prePage.data.orderName);
              wx.navigateBack({
                delta: 1,
              })
            },2000)
          },
          fail (res) { 
            console.log("调用失败")
            wx.showToast({
              title: '支付失败',
              icon:'none'
            })
          }
        })
      }
    })
  },
})